package com.yunshang.shoppingmall.entity;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.authentication.AuthenticationProvider;
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.AuthenticationException;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.stereotype.Component;

@Component
public class AdminAuthenticationProvider implements AuthenticationProvider {

    @Autowired
    LoadUserService loadUserService;

    @Override
    public Authentication authenticate(Authentication authentication) throws AuthenticationException {
        String userName = authentication.getName();
        System.out.println("-------get-------->"+authentication.getDetails());
        String password = (String) authentication.getCredentials();
        UserDetails userDetails =loadUserService.loadUserByUsername(userName);
        System.out.println("---------usename------------>"+userDetails.getUsername()+"-----mima----"+userDetails.getPassword()+"--quanxian-"+userDetails.getAuthorities());
        return new UsernamePasswordAuthenticationToken(userDetails, password,  userDetails.getAuthorities());
    }

    @Override
    public boolean supports(Class<?> authentication) {
        return true;
    }


}
